/*************************************************************************
	> File Name: P1886.cpp
	> Author: 
	> Mail: 
	> Created Time: 
 ************************************************************************/
#include <iostream>
#include <deque>
#define MAX_N 1000000
using namespace std;

int arr[MAX_N + 5] = {0};
deque<int> q;

int main() {
    int n, k; cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> arr[i];
    for (int i = 1; i <= n; i++) {
        while (!q.empty() && arr[q.back()] > arr[i]) q.pop_back();
        q.push_back(i);
        if (i >= k) {
            while (!q.empty() && q.front() <= i - k) q.pop_front();
            cout << arr[q.front()] << " ";
        }
    }
    cout << endl;
    while (!q.empty()) q.pop_front();
    for (int i = 1; i <= n; i++) {
        while (!q.empty() && arr[q.back()] < arr[i]) q.pop_back();
        q.push_back(i);
        if (i >= k) {
            while (!q.empty() && q.front() <= i - k) q.pop_front();
            cout << arr[q.front()] << " ";
        }
    }
    cout << endl;
    return 0;
}
